package com.bodacn.keypoint;

import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Locale;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

public class ImageActivity extends AppCompatActivity {
    PhotoViewAttacher mAttacher;
    PhotoView photoView;
    String PhotoUrl="",language="";
    Button btn_ImageClose;


    //核心设置的代码
    protected void switchLanguage() {
        //读取配置文件-设置语言
        String STORE_NAME = "Settings";
        SharedPreferences settings = getSharedPreferences(STORE_NAME, MODE_PRIVATE);
        language=settings.getString("language",language);
        //获取资源
        Resources resources = getResources();
        Configuration config = resources.getConfiguration();
        DisplayMetrics dm = resources.getDisplayMetrics();
        switch (language) {
            case "zh":
                config.locale = new Locale("zh","");
                resources.updateConfiguration(config, dm);
                break;
            case "en":
                config.locale = new Locale("en","");
                resources.updateConfiguration(config, dm);
                break;
            case "km":
                config.locale = new Locale("km","");
                resources.updateConfiguration(config, dm);
                break;
            default:
                config.locale = new Locale("zh","");
                resources.updateConfiguration(config, dm);
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //切换语言
        switchLanguage();
        //启动Activity
        setContentView(R.layout.activity_image);
        //获取登陆参数
        Bundle paramBundle = this.getIntent().getExtras();
        PhotoUrl=paramBundle.getString("PhotoUrl");
        InitView();
    }

    private void InitView(){
        btn_ImageClose=(Button)findViewById(R.id.btn_Close);
        btn_ImageClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("onClick","准备关闭");
                //关闭当前Activity;
                finish();
            }
        });
        photoView=(PhotoView)findViewById(R.id.photoView);
        if (PhotoUrl.length()>0) {
            //读取本地图片;
            photoView.setImageBitmap(getLoacalBitmap(PhotoUrl));
            mAttacher = new PhotoViewAttacher(photoView);
            mAttacher.update();
        }
    }


    /**
     * @Title: getLoacalBitmap
     * @Description: 加载本地图片
     * @param @param url 本地路径
     * @param @return
     * @return Bitmap
     * @throws
     */
    public Bitmap getLoacalBitmap(String url) {
        if (url != null) {
            FileInputStream fis=null;
            try {
                fis = new FileInputStream(url);
                return BitmapFactory.decodeStream(fis); // /把流转化为Bitmap图片
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return null;
            }finally{
                if(fis!=null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    fis=null;
                }
            }
        } else {
            return null;
        }
    }

}
